package nio.c1;


import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import static com.ymfNetty.nio.c2.ByteBufferUtil.debugAll;


@Slf4j
public class ChannelDemo1 {

    public  static void main(String[] args) {
        try(FileChannel channel = new FileInputStream("data.txt").getChannel()) {
            ByteBuffer buffer = ByteBuffer.allocate(10);
            while (true){
                int len = channel.read(buffer);
                if (len == -1) {
                    break;
                }
                //切换到读模式
                buffer.flip();
                debugAll(buffer);
                while (buffer.hasRemaining()) {
                    byte b = buffer.get();
                    System.out.println((char)b);
                }
                //切换到写模式
                buffer.clear();
            }

        }
        catch (IOException e){

        }
    }
}
